package com._21cn._front.product.api;

import com._21cn._front.product.page.label.CarLabel;
import com._21cn.cms.common.Constants;
import com._21cn.cms.common.ErrorCode;
import com._21cn.cms.common.utils.ResponseUtil;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.http.HttpRequestInfo;
import com._21cn.product.entity.Car;
import com._21cn.product.entity.Cate;
import com._21cn.product.service.CarService;
import com._21cn.product.service.CateService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/8.
 */
@Controller
@RequestMapping({"/api/car"})
public class CarApiController {

    @Autowired(required = false)
    private CateService CateService;

    @Autowired(required = false)
    private CarLabel carLabel;

    @RequestMapping("/getSeriesList")
    public void getSeriesList(HttpServletRequest request, HttpServletResponse response, ModelMap model){
        try{
            HttpRequestInfo reqInfo = new HttpRequestInfo(request);
            Integer brandId = reqInfo.getIntParameter("brandId", 0);
            Map<String, Object> params = new HashMap<String, Object>();
            if(brandId==0){
                params.put("eqIsShow", Constants.IS_SHOW);
            }
            params.put("eqParentId", brandId);
            params.put("eqTypeId", Constants.CATE_TYPE_SERIES);
            params.put("eqStatus", Constants.STATUS_ENABLE);
            PageList<Cate> brandList = CateService.getList(params, 1, Constants.DEFAULT_PAGE_SIZE_COMMON);
            Map<String, Object> resultMap = new HashMap<String, Object>();

            resultMap.put("ret", ErrorCode.SUCCESS_CODE);
            resultMap.put("total", Integer.valueOf(brandList.getPageTurn() != null ? brandList.getPageTurn().getRowCount().intValue() : 0));
            resultMap.put("list", brandList);
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Object.class,  "ret", "msg", "list", "id", "name", "nameShort", "sortNum", "total" );
            String responseResult = JSONObject.toJSONString(resultMap, filter, SerializerFeature.WriteDateUseDateFormat);
            ResponseUtil.returnJsonMsg(response, responseResult);
            return;

        }catch (Exception e){
            ResponseUtil.returnJsonMsg(response, ErrorCode.ERROR_SERVER_EXCEPTION, "服务器处理错误", null);
        }
    }

    @RequestMapping({"/getPledgeCarList"})
    public void getListByColumns(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        try{
            HttpRequestInfo reqInfo = new HttpRequestInfo(request);
            String brandId = reqInfo.getParameter("brandId", "0");
            String seriesId = reqInfo.getParameter("seriesId", "0");
            String otherId = reqInfo.getParameter("otherId", "0");
            String gtPrice = reqInfo.getParameter("gtPrice", "0");
            String ltPrice = reqInfo.getParameter("ltPrice", "0");
            String keyword = reqInfo.getParameter("keyword", "0");
            String sortRank = reqInfo.getParameter("sortRank", "0");
            String pageNo = reqInfo.getParameter("pageNo", "0");
            String pageSize = reqInfo.getParameter("pageSize", "0");
            PageList<Car> list = carLabel.getPledgeCarList(brandId, seriesId, otherId, gtPrice, ltPrice, keyword, sortRank, pageNo, pageSize);

            Map resultMap = new HashMap();
            resultMap.put("ret", ErrorCode.SUCCESS_CODE);
            resultMap.put("total", Integer.valueOf(list.getPageTurn() != null ? list.getPageTurn().getRowCount().intValue() : 0));
            resultMap.put("list", list);

            JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM";
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Object.class, new String[] { "ret", "total", "list", "id", "name","firstPicUrl", "seriesName", "secondPrice","dateOnBoard","odoMeter","status" });
            JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
            String responseResult = JSONObject.toJSONString(resultMap, filter,  SerializerFeature.WriteDateUseDateFormat);
            ResponseUtil.returnJsonMsg(response, responseResult);
            return;
        }catch (Exception e){
            ResponseUtil.returnJsonMsg(response, 1, "服务器处理错误", null);
        }
    }

    @RequestMapping({"/getSecondCarList"})
    public void getSecondCarList(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        try{
            HttpRequestInfo reqInfo = new HttpRequestInfo(request);
            String brandId = reqInfo.getParameter("brandId", "0");
            String seriesId = reqInfo.getParameter("seriesId", "0");
            String otherId = reqInfo.getParameter("otherId", "0");
            String gtPrice = reqInfo.getParameter("gtPrice", "0");
            String ltPrice = reqInfo.getParameter("ltPrice", "0");
            String keyword = reqInfo.getParameter("keyword", "0");
            String sortRank = reqInfo.getParameter("sortRank", "0");
            String pageNo = reqInfo.getParameter("pageNo", "0");
            String pageSize = reqInfo.getParameter("pageSize", "0");
            PageList<Car> list = carLabel.getSecondCarList(brandId, seriesId, otherId, gtPrice, ltPrice, keyword, sortRank, pageNo, pageSize);

            Map resultMap = new HashMap();
            resultMap.put("ret", ErrorCode.SUCCESS_CODE);
            resultMap.put("total", Integer.valueOf(list.getPageTurn() != null ? list.getPageTurn().getRowCount().intValue() : 0));
            resultMap.put("list", list);

            JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM";
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Object.class, new String[] { "ret", "total", "list", "id", "name","firstPicUrl", "seriesName", "secondPrice","dateOnBoard","odoMeter" });

            String responseResult = JSONObject.toJSONString(resultMap, filter,  SerializerFeature.WriteDateUseDateFormat);
            ResponseUtil.returnJsonMsg(response, responseResult);
            return;
        }catch (Exception e){
            ResponseUtil.returnJsonMsg(response, 1, "服务器处理错误", null);
        }
    }


    @RequestMapping({"/getPresellCarList"})
    public void getPresellCarList(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        getSecondCarList(request, response, model);
    }
}
